package com.ludomancy.starwave;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import com.ludomancy.gamelib.ResourceManager;

public class SliderShip extends Ship
{
	protected int directionY = 1;
	
	public SliderShip(Player owner, int px, int py)
	{
		super(owner, px, py);
		type = SLI;
	}
	
	public void paintInYard(Graphics g, int x, int y)
	{
		Image image = ResourceManager.getImage(directionY > 0 ? "slider.png" : "slider-up.png");
		g.drawImage(image, x, y, null);
	}

	public String getDescription()
	{
		return "The Slider moves diagonally.";
	}
	
	protected Point[] getFiringArc()
	{
		if(canMove())
		{
			Point path = getPath(px,py)[0];
			return new Point[] {new Point(path.x-px, path.y-py)};
		}
		return new Point[0];
	}
	
	public Point[] getPath(int tilex, int tiley)
	{
		Point[] path = new Point[px];
		int cury = directionY;
		int ndx = 0;
		int tpx = tilex;
		int tpy = tiley;
		while(canMove(tpx, tpy, cury))
		{
			if((tpy == 0 && cury < 0) || (tpy == Game.GRID_HEIGHT-1 && cury > 0))
			{
				cury = -cury;
			}
			tpx += owner.direction;
			tpy += cury;
			path[ndx++] = new Point(tpx, tpy);
		}
		return path;
	}
	
	private boolean canMove(int fromx, int fromy, int diry)
	{
		return fromx > 0 && fromx < Game.GRID_WIDTH-1;
	}
	
	protected Point nextPosition()
	{
		Point offset = getPath(px,py)[0];
		directionY = offset.y - py;
		return new Point(offset.x, offset.y);
	}
	
	public boolean isMover()
	{
		return true;
	}

	protected boolean canMove()
	{
		Point[] path = getPath(px,py);
		if(path.length == 0)
		{
			return false;
		}
		return canMoveHere(path[0].x, path[0].y);
	}

}
